package com.das.mechanic_base.mvp.view.create;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.adapter.create.ServiceInfoAdapter;
import com.das.mechanic_base.adapter.create.ServiceInfoCarUserAdapter;
import com.das.mechanic_base.adapter.create.ServiceInfoFirstAdapter;
import com.das.mechanic_base.adapter.create.ServiceStatusAdapter;
import com.das.mechanic_base.app.IAloneEventCallback;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.create.MapReceiveStatusBean;
import com.das.mechanic_base.bean.create.SaveWorkSuccessBean;
import com.das.mechanic_base.bean.create.ServiceInfoBean;
import com.das.mechanic_base.bean.even.DeleteFriends;
import com.das.mechanic_base.bean.even.OptionMainTainPlan;
import com.das.mechanic_base.bean.main.CarPicAndColorBean;
import com.das.mechanic_base.bean.main.CarSelectBean;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.init.AutoX3SDK;
import com.das.mechanic_base.mvp.contract.create.X3ServiceInfoContract;
import com.das.mechanic_base.mvp.presenter.create.X3ServiceInfoPresenter;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.das.mechanic_base.widget.X3AloneBeforeSignDialog;
import com.das.mechanic_base.widget.X3BottomAddStaffDialog;
import com.das.mechanic_base.widget.X3BottomForwardDialog;
import com.das.mechanic_base.widget.X3BottomHomeCarDialog;
import com.das.mechanic_base.widget.X3BottomMaintainPlaneDialog;
import com.das.mechanic_base.widget.X3BottomStaffDialog;
import com.das.mechanic_base.widget.X3ModifyHeaderView;
import com.das.mechanic_base.widget.X3PointDialog;
import com.das.mechanic_base.widget.X3ReAloneWindow;
import com.hjq.toast.ToastUtils;
import com.kproduce.roundcorners.RoundButton;
import com.kproduce.roundcorners.RoundImageView;
import com.kproduce.roundcorners.RoundRelativeLayout;
import com.kproduce.roundcorners.RoundView;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

import static com.das.mechanic_base.app.X3Constent.ALONE_SIGN_REQUEST;
import static com.das.mechanic_base.app.X3Constent.CODE_CAMERA_VIN_REQUEST;


@Route(path = X3AppRouterConstants.Router.SERVICE_INFO)
public class X3ServiceInfoActivity extends X3BaseActivity<X3ServiceInfoPresenter> implements X3ServiceInfoContract.View, ServiceInfoAdapter.IOnClickOrder, X3BottomHomeCarDialog.IOnClickCar,
        X3BottomAddStaffDialog.IOnRefresh, X3BottomStaffDialog.IOnSaveWorkBase, IAloneEventCallback,
        ServiceInfoCarUserAdapter.IOnClickCarUser, ServiceInfoFirstAdapter.IOnClickJumpFirst {

    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.rl_header)
    RelativeLayout rl_header;
    @BindView(R2.id.tv_alone)
    TextView tv_alone;
    @BindView(R2.id.tv_work)
    TextView tv_work;
    @BindView(R2.id.rv_alone)
    RoundView rv_alone;
    @BindView(R2.id.rv_work)
    RoundView rv_work;
    @BindView(R2.id.rl_alone_add)
    RelativeLayout rl_alone_add;
    @BindView(R2.id.rl_work_add)
    RelativeLayout rl_work_add;
    @BindView(R2.id.rlv_item)
    RecyclerView rlv_item;
    @BindView(R2.id.ll_staff)
    LinearLayout ll_staff;
    @BindView(R2.id.fl_work)
    FrameLayout fl_work;
    @BindView(R2.id.rl_bottom)
    LinearLayout rl_bottom;
    @BindView(R2.id.fl_empty)
    FrameLayout fl_empty;
    @BindView(R2.id.nv_content)
    NestedScrollView nv_content;
    @BindView(R2.id.rlv_service)
    RecyclerView rlv_service;
    @BindView(R2.id.rl_status)
    RoundRelativeLayout rl_status;
    @BindView(R2.id.v_alpha)
    View v_alpha;
    @BindView(R2.id.rl_all)
    RelativeLayout rl_all;
    @BindView(R2.id.tv_all)
    TextView tv_all;
    @BindView(R2.id.iv_all)
    ImageView iv_all;
    @BindView(R2.id.rv_check)
    RoundButton rv_check;
    @BindView(R2.id.rv_end)
    RoundButton rv_end;
    @BindView(R2.id.rl_user)
    RelativeLayout rl_user;
    @BindView(R2.id.rl_car)
    RelativeLayout rl_car;
    @BindView(R2.id.tv_staff_add)
    TextView tv_staff_add;
    @BindView(R2.id.v_staff)
    View v_staff;
    @BindView(R2.id.vp_main)
    ViewPager2 vp_main;
    @BindView(R2.id.iv_user)
    RoundImageView iv_user;
    @BindView(R2.id.iv_car)
    ImageView iv_car;
    @BindView(R2.id.rlv_alone)
    RecyclerView rlv_alone;
    @BindView(R2.id.v_alone_line)
    View v_alone_line;
    @BindView(R2.id.tv_alone_edit)
    TextView tv_alone_edit;

    private long receiveBaseId;
    private ServiceInfoAdapter infoAdapter;
    private List<ServiceInfoBean.WorkBaseBean> aloneList = new ArrayList<>();
    private List<ServiceInfoBean.WorkBaseBean> workList = new ArrayList<>();
    private ServiceInfoBean infoBean;
    private X3BottomStaffDialog staffDialog;
    private ServiceStatusAdapter statusAdapter;
    private String title;
    private long carId;
    private boolean detectionFirstOrNot;
    private X3BottomHomeCarDialog homeCarDialog;
    private String carNum;
    private X3BottomAddStaffDialog addStaffDialog;
    //保养计划
    private X3BottomMaintainPlaneDialog maintainPlaneDialog;
    private boolean isSdk;

    public boolean isDetection = true;
    private ServiceInfoCarUserAdapter carUserAdapter;
    private ServiceInfoFirstAdapter firstAdapter;
    private boolean isFirst,isPlanFirst;
    private boolean isCreate;
    private X3PointDialog closeCarOrderDialog;

    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.activity_service_info;
    }

    @Override
    protected void initView() {
        X3StatusBarUtil.darkMode(this);
        rl_header.setPadding(0,X3StatusBarUtil.getStatusBarHeight(this),0,0);
        rl_header.setBackgroundColor(this.getResources().getColor(R.color.bg_F5F5F7));
        tv_title.setText(getString(R.string.x3_main_look_work));
        receiveBaseId = getIntent().getLongExtra("receiveBaseId", 0L);
        carId = getIntent().getLongExtra("carId", 0L);
        detectionFirstOrNot = getIntent().getBooleanExtra("detectionFirstOrNot", false);
        isCreate = getIntent().getBooleanExtra("isCreate", false);

        isSdk = (boolean) SpHelper.getData(X3Constent.IS_OPEN_SDK, false);

        title = getIntent().getStringExtra("title");

        vp_main.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        carUserAdapter = new ServiceInfoCarUserAdapter(this);
        vp_main.setAdapter(carUserAdapter);
        carUserAdapter.setiOnClickCarUser(this);
        vp_main.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                rl_user.setBackground(position == 0 ? null : ContextCompat.getDrawable(X3ServiceInfoActivity.this,R.mipmap.x3_car_or_user_select));
                rl_car.setBackground(position == 0 ? ContextCompat.getDrawable(X3ServiceInfoActivity.this,R.mipmap.x3_car_or_user_select) : null);
            }
        });

        rlv_item.setLayoutManager(new LinearLayoutManager(this));
        infoAdapter = new ServiceInfoAdapter(this);
        rlv_item.setAdapter(infoAdapter);
        infoAdapter.setiOnClickOrder(this);

        rlv_service.setLayoutManager(new LinearLayoutManager(this));
        statusAdapter = new ServiceStatusAdapter(this);
        rlv_service.setAdapter(statusAdapter);

        rlv_alone.setLayoutManager(new LinearLayoutManager(this));
        firstAdapter = new ServiceInfoFirstAdapter(this);
        rlv_alone.setAdapter(firstAdapter);
        firstAdapter.setiOnClickJumpFirst(this);

        isFirst = true;
        isPlanFirst = true;
    }

    @Override
    protected X3ServiceInfoPresenter createPresenter() {
        return new X3ServiceInfoPresenter();
    }

    @OnClick({R2.id.iv_back,R2.id.tv_alone,R2.id.tv_work,R2.id.rv_end,R2.id.rl_alone_add,
            R2.id.rl_work_add,R2.id.rl_all,R2.id.rl_car,R2.id.rl_user,R2.id.tv_staff_add,
            R2.id.ll_staff,R2.id.rv_check,R2.id.tv_alone_edit,R2.id.tv_forward})
    public void onViewClick(View view) {
        if (view.getId() == R.id.iv_back) {
            finish();

        }else if (view.getId() == R.id.tv_alone) {
            isDetection = true;
            tv_alone.setTextColor(ContextCompat.getColor(this,R.color.c333));
            tv_work.setTextColor(ContextCompat.getColor(this,R.color.bg_B1B3BD));
            rv_alone.setVisibility(View.VISIBLE);
            rv_work.setVisibility(View.GONE);
            rl_work_add.setVisibility(View.GONE);
            if (rl_alone_add != null){
                rl_alone_add.setVisibility(infoBean.payStatus ? View.GONE : View.VISIBLE);
            }
            // 切换检测单

            rlv_item.setVisibility(X3StringUtils.isListEmpty(aloneList) ? View.GONE : View.VISIBLE);
            showEmptyWork(true,aloneList);
            if (infoAdapter != null) {
                infoAdapter.changeServiceList(aloneList,true);
            }

        }else if (view.getId() == R.id.tv_work) {
            isDetection = false;
            tv_work.setTextColor(ContextCompat.getColor(this,R.color.c333));
            tv_alone.setTextColor(ContextCompat.getColor(this,R.color.bg_B1B3BD));
            rv_work.setVisibility(View.VISIBLE);
            rv_alone.setVisibility(View.GONE);
            rl_alone_add.setVisibility(View.GONE);
            if (infoBean != null){
                rl_work_add.setVisibility(infoBean.payStatus ? View.GONE : View.VISIBLE);
            }

            // 切换施工单
            rlv_item.setVisibility(X3StringUtils.isListEmpty(workList) ? View.GONE : View.VISIBLE);
            showEmptyWork(false,workList);
            if (infoAdapter != null) {
                infoAdapter.changeServiceList(workList,false);
            }

        }else if (view.getId() == R.id.rv_end) {
            // 完成接车单
            closeCarOrder();
        }else if (view.getId() == R.id.rl_alone_add) {
            if (infoBean != null && infoBean.payStatus) {
                rl_alone_add.setVisibility(View.GONE);
                return;
            }
            // 添加检测单
            Intent intent = new Intent(this,SelectWorkActivity.class);
            intent.putExtra("receiveBaseId",receiveBaseId);
            intent.putExtra("isDetection",isDetection);
            startActivity(intent);

        }else if (view.getId() == R.id.rl_work_add) {
            if (infoBean != null && infoBean.payStatus) {
                rl_work_add.setVisibility(View.GONE);
                return;
            }
            // 添加施工单
            Intent intent = new Intent(this,SelectWorkActivity.class);
            intent.putExtra("receiveBaseId",receiveBaseId);
            intent.putExtra("isDetection",isDetection);
            startActivity(intent);

        }else if (view.getId() == R.id.rl_all) {
            if (getString(R.string.look_all).equals(tv_all.getText().toString())) {
                // 展开
                v_alpha.setVisibility(View.GONE);
                tv_all.setText(getString(R.string.task_gone));
                if (statusAdapter != null) {
                    statusAdapter.changeExpand(true);
                }
                iv_all.setImageResource(R.mipmap.x3_up);
            }else {
                // 收起
                v_alpha.setVisibility(View.VISIBLE);
                tv_all.setText(getString(R.string.look_all));
                if (statusAdapter != null) {
                    statusAdapter.changeExpand(false);
                }
                iv_all.setImageResource(R.mipmap.x3_plan_down);
            }
        }else if (view.getId() == R.id.rl_user) {
            vp_main.setCurrentItem(1);
            rl_user.setBackground(ContextCompat.getDrawable(this,R.mipmap.x3_car_or_user_select));
            rl_car.setBackground(null);
            
        }else if (view.getId() == R.id.rl_car) {
            vp_main.setCurrentItem(0);
            rl_car.setBackground(ContextCompat.getDrawable(this,R.mipmap.x3_car_or_user_select));
            rl_user.setBackground(null);

        }else if (view.getId() == R.id.tv_staff_add || view.getId() == R.id.ll_staff) {
            // 添加专属服务顾问
            if (addStaffDialog == null) {
                addStaffDialog = new X3BottomAddStaffDialog(this);
                addStaffDialog.setiOnRefresh(this);
            }
            addStaffDialog.show();
            addStaffDialog.showAllStaff(carId);
        }else if (view.getId() == R.id.rv_check) {
            // 查看报价单
            Bundle bundle = new Bundle();
            bundle.putString("title","查看报价单");
            bundle.putBoolean("isDark",true);
            bundle.putString("path", X3Constent.CHECK_RECEIVE_QUOTATION + receiveBaseId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);

        }else if (view.getId() == R.id.tv_alone_edit) {
            // 初检
            startToFirstAlone();
        } else if (view.getId() == R.id.tv_forward) {
            // 转发"车主服务-url"
            //http://localhost:8085/step/index.html?id=接车单id
            String url = X3Constent.getShareStepUrl()+ receiveBaseId;
            X3BottomForwardDialog x3BottomForwardDialog = new X3BottomForwardDialog(this,url);
            x3BottomForwardDialog.show();
        }
    }

    //关闭接车单
    private void closeCarOrder(){
        if (closeCarOrderDialog == null) {
            closeCarOrderDialog = new X3PointDialog(this, "", this.getString(R.string.x3_car_close),
                    this.getString(R.string.x3_no_tips), this.getString(R.string.x3_yes_tips));
        }
        closeCarOrderDialog.show();
        closeCarOrderDialog.setiBtnClick(new X3PointDialog.IBtnClick() {
            @Override
            public void iBtnAffirmClick() {
                closeCarOrderDialog.dismiss();
                if (mPresenter != null) {
                    mPresenter.closeReceiveBase(receiveBaseId);
                }
            }

            @Override
            public void iBtnCancelClick() {
            }
        });
    }

    private void startToFirstAlone() {
        Intent intent = new Intent(this, X3FirstAloneActivity.class);
        intent.putExtra("receiveBaseId",receiveBaseId);
        intent.putExtra("isFirst",isFirst);
        startActivity(intent);
        isFirst = false;
    }

    @Override
    public void getReceiveSuccess(ServiceInfoBean infoBean) {
        showEmptyLayout(infoBean);
        if (infoBean == null) {
            return;
        }
        this.infoBean = infoBean;
        this.carId = infoBean.carId;
        this.carNum = infoBean.carNum;

        aloneList = infoBean.workBaseVOListDetection;
        workList = infoBean.workBaseVOListMaintain;

        rv_check.setText(infoBean.hasReceivePrice ? getString(R.string.x3_check_pay_order) : getString(R.string.x3_go_check_pay_order));
        rv_end.setVisibility(infoBean.payStatus ? View.GONE : View.VISIBLE);

        rl_bottom.setVisibility((rv_check.getVisibility() == View.GONE && rv_end.getVisibility() == View.GONE) ? View.GONE : View.VISIBLE);

        if (X3StringUtils.isListEmpty(aloneList)) {
            aloneList = new ArrayList<>();
        }
        if (X3StringUtils.isListEmpty(workList)) {
            workList = new ArrayList<>();
        }

        if (carUserAdapter != null) {
            carUserAdapter.changeServiceInfo(infoBean);
        }
        changeStaffList();

        if (isDetection) {
            tv_alone.performClick();
        }else {
            tv_work.performClick();
        }

        // 创建接车单的时候才会执行
        if (isCreate) {
            if (infoBean.maintenancePlanAmount > 0 && isPlanFirst) {
                onSubViewClick(0);
            }

            if (detectionFirstOrNot && isFirst) {
                startToFirstAlone();
            }
        }

        if (!infoBean.needUpdateCarStyle) {
            return;
        }
        // 覆盖车型数据
        if(homeCarDialog != null && homeCarDialog.isShowing()){
            return;
        }
        homeCarDialog = new X3BottomHomeCarDialog(this);
        homeCarDialog.show();
        homeCarDialog.setReceiveId(receiveBaseId,infoBean.carNum);
        homeCarDialog.setChangeCarType();
        homeCarDialog.setiOnClick(this);
        //获取接车单数据
        if (mPresenter != null) {
            mPresenter.getReceiveBase(receiveBaseId);
        }

    }

    @Override
    public void getReceiveStatusListSuccess(MapReceiveStatusBean statusBean) {
        if (statusBean == null) {
            rl_status.setVisibility(View.GONE);
            return;
        }
        List<MapReceiveStatusBean.ReceiveGraphEntityBean> statusList = statusBean.receiveGraphEntityList;
        if (X3StringUtils.isListEmpty(statusList)) {
            rl_status.setVisibility(View.GONE);
            return;
        }
        if (statusAdapter != null) {
            statusAdapter.changeStatusList(statusList);
        }

        v_alpha.setVisibility(statusList.size() <= 3 ? View.GONE : View.VISIBLE);
        rl_all.setVisibility(statusList.size() <= 3 ? View.GONE : View.VISIBLE);

    }

    @Override
    public void getReceiveBase(HomeMainCarBean mainCarBean) {
        this.detectionFirstOrNot = mainCarBean.isDetectionFirstOrNot();
        if (mainCarBean == null) {
            return;
        }
        if (homeCarDialog != null && homeCarDialog.isShowing()) {
            homeCarDialog.changeHomeMainData(mainCarBean);
        }
    }

    @Override
    public void updateCarTypeSuccess() {
        hindLoading();
        if (homeCarDialog != null) {
            homeCarDialog.dismiss();
        }
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
    }
    @Override
    public void updateCarTypeFail() {
        hindLoading();
        if (homeCarDialog != null) {
            homeCarDialog.resetBtn();
        }
    }

    @Override
    public void uploadCarPicSuccess(CarPicAndColorBean colorBean) {
        hindLoading();
        if (colorBean == null) {
            X3ToastUtils.showMessage(getString(R.string.x3_car_cut_fail));
            if (homeCarDialog != null) {
                homeCarDialog.uploadCarCutSuccess(null);
            }
        }else {
            if (homeCarDialog != null) {
                homeCarDialog.uploadCarCutSuccess(colorBean);
            }
        }
    }

    private void hindLoading(){
        if (viewProxy != null) {
            viewProxy.hideLoading();
        }
    }

    @Override
    public void getVin(String vin) {
        if (X3StringUtils.isEmpty(vin)) {
            X3ToastUtils.showMessage("" + getString(R.string.x3_no_recognize_vin));
            return;
        }
        if(homeCarDialog != null){
            homeCarDialog.setVin(vin);
        }
    }

    @Override
    public void closeReceiveBaseSuccess() {
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
        refreshLastPage();
    }

    /**
     * TODO 获取初检信息
     * @param mList
     */
    @Override
    public void getDetectionFirstInfoSuccess(List<DetectionNewBean> mList) {
        if (X3StringUtils.isListEmpty(mList)) {
            rlv_alone.setVisibility(View.GONE);
            v_alone_line.setVisibility(View.GONE);
            return;
        }

        List<DetectionNewBean> afterList = new ArrayList<>();
        for (int j = 0; j < mList.size(); j++) {
            List<DetectionNewBean.QuestionToItemDetectionBean> questionList = mList.get(j).questionToItemDetectionFirstVOList;
            String questionToSystemSn = mList.get(j).questionToSystemSn;
            String content = "";

            if (!X3StringUtils.isListEmpty(questionList)) {
                for (int i = 0; i < questionList.size(); i++) {
                    List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> detectionBean = questionList.get(i).questionList;
                    if ("sys_thing_001".equals(questionToSystemSn) || "sys_tools_001".equals(questionToSystemSn)) {

                        for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : detectionBean) {
                            if (!questionBean.selected) {
                                continue;
                            }
                            if (X3StringUtils.isEmpty(content)) {
                                content = questionBean.answer;
                            } else {
                                content = content + ", " + questionBean.answer;
                            }
                        }

                    } else if (!X3StringUtils.isListEmpty(detectionBean)){
                        for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionBean : detectionBean) {
                            if (X3StringUtils.isEmpty(content)) {
                                content = questionBean.answer;
                            } else {
                                content = content + ", " + questionBean.answer;
                            }
                        }
                    }
                }
            }

            if (X3StringUtils.isEmpty(content) && !"sys_sign_001".equals(questionToSystemSn)) {
                continue;
            }
            if ("sys_sign_001".equals(questionToSystemSn)) {
                 if (X3StringUtils.isEmpty(mList.get(j).url)){
                     tv_alone_edit.setVisibility(View.VISIBLE);
                 }else{
                     tv_alone_edit.setVisibility(View.INVISIBLE);
                 }
            }
            afterList.add(mList.get(j));
        }
        if (afterList.size() == 1 && "sys_sign_001".equals(afterList.get(0).questionToSystemSn)){
            rlv_alone.setVisibility(View.GONE);
            v_alone_line.setVisibility(View.GONE);
            tv_alone_edit.setVisibility(View.VISIBLE);
        }else{
            rlv_alone.setVisibility(X3StringUtils.isListEmpty(afterList) ? View.GONE : View.VISIBLE);
            v_alone_line.setVisibility(X3StringUtils.isListEmpty(afterList) ? View.GONE : View.VISIBLE);
        }
        if (firstAdapter != null) {
            firstAdapter.changeSerivceInfoFirst(afterList);
        }
    }

    @Override
    public void saveDetectionFirstInfoSuccess() {

    }

    /**
     * 显示专属服务顾问
     */
    private void changeStaffList() {
        X3GlideNewUtils.loadNormalImage(this, X3StringUtils.getImageUrl(infoBean.carBrandUrl),iv_car, R.mipmap.x3_car_icon_no);

        String imgUrl = infoBean.imgUrl;
        if (X3StringUtils.isEmpty(imgUrl)) {
            imgUrl = X3ModifyHeaderView.setName(infoBean.carOwnerName,"",this);
            X3GlideNewUtils.loadNormalImage(this,imgUrl,iv_user, R.mipmap.x3_car_owner_header);
        }else {
            X3GlideNewUtils.loadNormalImage(this,X3StringUtils.getImageUrl(imgUrl),iv_user, R.mipmap.x3_car_owner_header);
        }

        ll_staff.removeAllViews();
        List<ServiceInfoBean.StaffBaseListBean> staffBaseList = infoBean.staffBaseVOList;

        tv_staff_add.setVisibility((!X3StringUtils.isListEmpty(staffBaseList) && staffBaseList.size() >= 2) ? View.INVISIBLE : View.VISIBLE);
        if (X3StringUtils.isListEmpty(staffBaseList)) {
            // 显示空白占位
            ll_staff.setVisibility(View.GONE);
            v_staff.setVisibility(View.GONE);
            return;
        }
        ll_staff.setVisibility(View.VISIBLE);
        v_staff.setVisibility(View.VISIBLE);
        ll_staff.removeAllViews();
        for (ServiceInfoBean.StaffBaseListBean staffBean : staffBaseList) {
            View headerView = View.inflate(this,R.layout.x3_service_order_staff_item,null);
            RoundImageView rv_staff = headerView.findViewById(R.id.rv_staff);
            TextView tv_staff_name = headerView.findViewById(R.id.tv_staff_name);
            TextView tv_staff_post = headerView.findViewById(R.id.tv_staff_post);

            X3GlideNewUtils.loadNormalImage(this,X3StringUtils.getImageUrl(staffBean.portrait),rv_staff,R.mipmap.x3_car_owner_header);
            tv_staff_name.setText(X3StringUtils.isEmpty(staffBean.staffBaseName) ? "" : staffBean.staffBaseName);
            tv_staff_post.setText(X3StringUtils.isEmpty(staffBean.postBaseName) ? "" : staffBean.postBaseName);
            LinearLayout.LayoutParams headerLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            headerLayout.topMargin = X3ScreenUtils.dipToPx(20, this);
            ll_staff.addView(headerView,headerLayout);
        }
    }

    @Override
    public void iOnClickOrder(long workBaseId,boolean isDetection) {
        if (isDetection) {
            AutoX3SDK.startHealthCheck(workBaseId,receiveBaseId, X3MyApp.getInstance().iAloneEventCallback);
        }else {
            AutoX3SDK.startWorkOrder(workBaseId,receiveBaseId, X3MyApp.getInstance().iAloneEventCallback);
        }
    }

    @Override
    public void iOnSaveWork(ServiceInfoBean.WorkBaseBean workBaseBean) {
        if (staffDialog == null) {
            staffDialog = new X3BottomStaffDialog(this);
            staffDialog.setiOnSaveWork(this);
        }
        staffDialog.show();
        staffDialog.giveServiceVariantBean(receiveBaseId,workBaseBean,isDetection);
    }

    @Override
    public void iOnAssignStaff(ServiceInfoBean.WorkBaseBean workBaseBean) {
        staffDialog = new X3BottomStaffDialog(this);
        staffDialog.setiOnSaveWork(this);
        staffDialog.show();
        staffDialog.assignStaff(workBaseBean,isDetection);
    }

    @Override
    public void iOnJumpToReport(ServiceInfoBean.WorkBaseBean workBaseBean, boolean isDetection) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isDark",true);
        bundle.putString("title",isDetection ? "查看检测报告页" : "查看施工报告页");
        bundle.putString("path", isDetection ? X3Constent.HOME_FINISH_DETAIL_MAIN_URL + "workBaseId=" + workBaseBean.workBaseId + "&carNum=" + carNum : X3Constent.HOME_FINISH_DETAIL_URL + "id=" + workBaseBean.workBaseId);
        X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
    }

    @Override
    public void iOnClickReAlone(View v ,long workBaseId,int position){
        X3ReAloneWindow x3ReAloneWindow = new X3ReAloneWindow(this);
        x3ReAloneWindow.setiOnPopClick(new X3ReAloneWindow.IPopClick() {
            @Override
            public void onPopDismiss() {
                if (infoAdapter != null){
                    infoAdapter.notifyItemChanged(position);
                }
            }

            @Override
            public void reAlone() {
                staffDialog = new X3BottomStaffDialog(X3ServiceInfoActivity.this);
                staffDialog.setiOnSaveWork(X3ServiceInfoActivity.this);
                staffDialog.show();
                staffDialog.giveServiceVariantBean(receiveBaseId,aloneList.get(position),isDetection,true);
            }
        });
        x3ReAloneWindow.showPopupWindow(v,false);
    }

    /**
     * 展示检测单或者施工的空白的占位图
     * @param isDetection
     */
    private void showEmptyWork(boolean isDetection,List<ServiceInfoBean.WorkBaseBean> mList) {
        if (!X3StringUtils.isListEmpty(mList)) {
            fl_work.setVisibility(View.GONE);
            return;
        }
        fl_work.setVisibility(View.VISIBLE);
        fl_work.removeAllViews();
        View emptyView = View.inflate(this,R.layout.x3_empty_layout,null);
        TextView tv_empty = emptyView.findViewById(R.id.tv_empty);
        ImageView iv_empty = emptyView.findViewById(R.id.iv_empty);
        iv_empty.setImageResource(R.mipmap.x3_home_no_order);
        tv_empty.setText(isDetection ? getString(R.string.not_have_alone_order) : getString(R.string.not_have_work_order));
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(X3ScreenUtils.dipToPx(100, this), X3ScreenUtils.dipToPx(100, this));
        layoutParams.topMargin = X3ScreenUtils.dipToPx(30, this);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
        iv_empty.setLayoutParams(layoutParams);
        LinearLayout.LayoutParams emptyLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        emptyLayout.bottomMargin = X3ScreenUtils.dipToPx(20, this);
        fl_work.addView(emptyView,emptyLayout);
    }

    /**
     * 展示检测单或者施工的空白的占位图
     * @param infoBean
     */
    private void showEmptyLayout(ServiceInfoBean infoBean) {
        if (infoBean != null) {
            fl_empty.setVisibility(View.GONE);
            nv_content.setVisibility(View.VISIBLE);
            return;
        }
        nv_content.setVisibility(View.GONE);
        rl_bottom.setVisibility(View.GONE);
        fl_empty.setVisibility(View.VISIBLE);
        fl_empty.removeAllViews();
        View emptyView = View.inflate(this,R.layout.x3_empty_layout,null);
        TextView tv_empty = emptyView.findViewById(R.id.tv_empty);
        tv_empty.setText(getString(R.string.x3_server_http_error));
        LinearLayout.LayoutParams emptyLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        emptyLayout.topMargin = X3ScreenUtils.dipToPx(120, this);
        fl_empty.addView(emptyView,emptyLayout);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetWork(DeleteFriends content) {
        if (content == null) {
            return;
        }
        String type = content.getType();
        if (X3StringUtils.isEmpty(type)) {
            return;
        }
        switch (type) {
            case "Refresh_Service":
                if (mPresenter != null) {
                    mPresenter.getReceiveForMechanic(receiveBaseId);
                }
                break;
            case "Create_Work_Order":
                isDetection = content.isDetection;
            case "UPDATE_CNACEL_ORDER":
            case "CAR_BIND_CODE":
            case "REFRESH_GROUND":
            case "CREATE_TASK_START":
            case "REFRESH_TASK":
            case "UPDATE_START_ORDER":
                refreshLastPage();
                if (mPresenter != null) {
                    mPresenter.getReceiveForMechanic(receiveBaseId);
                }
                break;
            case "CarCut":
                String imgPath = content.getImgPath();
                long carId = content.getCarId();
                String carNum = content.getCarNum();

                if (homeCarDialog != null) {
                    X3ToastUtils.showMessage(getString(R.string.x3_upload_car_cut_notice));
                    homeCarDialog.changeLastCarCut(imgPath + "");
                    // 上传首页创建的缩略图 切割
                    if (mPresenter != null) {
                        mPresenter.uploadCarPic(carId,imgPath,carNum);
                    }
                }
                break;
            case "Refresh_First_Alone":
                // 刷新初检
                if (mPresenter != null) {
                    mPresenter.getDetectionFirstInfo(receiveBaseId);
                }
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeleteMainTainPlan(OptionMainTainPlan plan) {
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if(X3StringUtils.isEmpty(title)){
            MobclickAgent.onPageEnd(title);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
            mPresenter.getReceiveStatusList(receiveBaseId);
            mPresenter.getDetectionFirstInfo(receiveBaseId);
        }
        if(X3StringUtils.isEmpty(title)){
            MobclickAgent.onPageStart(title);
        }
    }

    @Override
    public void iOnClikCameraVin() {
        X3RouterCenter.startVin(this);
    }

    @Override
    public void iOnClickCarBtnNext(CarSelectBean selectBean) {
        if (mPresenter != null) {
            // 创建接车单，更新车主信息
            mPresenter.updateReceiveCar(selectBean, carNum);
        }
    }

    @Override
    public void iOnClickDismiss(long receiveId) {
        finish();
    }

    @Override
    public void iOnCancelCurrentPage() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data == null) {
            return;
        }

        switch (requestCode) {
            case CODE_CAMERA_VIN_REQUEST:
                if (homeCarDialog == null) {
                    return;
                }
                if(X3Constent.isChina()){
                    String vinResult = data.getStringExtra("vinResult");
                    System.out.println("识别vin码"+vinResult);
                    homeCarDialog.setVin(vinResult);
                }else {
                    String vinPath = data.getStringExtra("path");
                    Luban.with(this)
                            .load(vinPath)
                            .ignoreBy(100)
                            .setTargetDir(X3FileUtils.getStoragePath(this, X3FileUtils.MECHANIC_PHOTO_STORAGE_PATH))
                            .filter(path -> !(TextUtils.isEmpty(vinPath) || vinPath.toLowerCase().endsWith(".gif")))
                            .setCompressListener(new OnCompressListener() {
                                @Override
                                public void onStart() {

                                }

                                @Override
                                public void onSuccess(File file) {
                                    if (mPresenter != null) {
                                        mPresenter.requestVin(file);
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {

                                }
                            }).launch();
                }

                break;
            case ALONE_SIGN_REQUEST:
                Bundle bundle = data.getExtras();
                if(bundle != null){
                    if (firstAdapter != null) {
                        firstAdapter.changeSerivceInfoFirst();
                    }
                }
                break;
        }
    }

    @Override
    public void iOnRefreshStaff() {
        // 刷新专属服务技师
        refreshLastPage();
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
    }

    private void refreshLastPage() {
        DeleteFriends deleteFriends = new DeleteFriends("refresh");
        EventBus.getDefault().post(deleteFriends);
    }

    @Override
    public void iOnSaveWorkSuccess(SaveWorkSuccessBean successBean,boolean isDetection) {
        refreshLastPage();
        if (successBean == null) {
            return;
        }
        if (!isSdk) {
            Bundle bundle = new Bundle();
            bundle.putString("title","创建施工单成功");
            bundle.putBoolean("isDark",true);
            long workBaseId = successBean == null ? 0 : successBean.workBaseId;
            String serviceCategoryName = successBean == null ? "" : successBean.serviceCategoryName;
            bundle.putString("path", X3Constent.SAVE_WORK_SUCCESS + "?id=" + receiveBaseId + "&workBaseId=" + workBaseId + "&serviceCategoryName=" + serviceCategoryName);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
            return;
        }
        if (isDetection) {
            AutoX3SDK.startHealthCheck(successBean.workBaseId,receiveBaseId,this);
        }else {
            AutoX3SDK.startWorkOrder(successBean.workBaseId,receiveBaseId,this);
        }
    }

    @Override
    public void iOnRefreshData() {

    }

    @Override
    public void iOnCancelWork(long workBaseId,String externalInspectionOrderId) {
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
    }

    @Override
    public void iOnCompleteWork(long workBaseId,String externalInspectionOrderId) {
        if (mPresenter != null) {
            mPresenter.getReceiveForMechanic(receiveBaseId);
        }
    }

    @Override
    public void onSubViewClick(int type) {
        if (type == 0){
            if (maintainPlaneDialog == null) {
                maintainPlaneDialog = new X3BottomMaintainPlaneDialog(this,carId,carNum);
            }
            maintainPlaneDialog.show();
            isPlanFirst = false;
        }
    }

    @Override
    public void iOnClickCarUser(int position) {
        if (infoBean == null) {
            return;
        }
        if (isSdk) {
            return;
        }
        if (!X3Utils.isFastClick()) {
            return;
        }
        if (position == 0) {
            // 车辆详情
            Bundle bundle = new Bundle();
            bundle.putBoolean("isDark",true);
            bundle.putString("title", "车辆详情页");
            bundle.putString("path", X3Constent.HOME_CAR_DETAIL_URL + infoBean.carId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);

        }else {
            if (infoBean == null || X3StringUtils.isEmpty(infoBean.mobile)) {
                // 跳转到绑定客户的页面
                Bundle bundle = new Bundle();
                bundle.putLong("carId",carId);
                X3RouterCenter.startNavigate(X3AppRouterConstants.Router.CAR_BIND_CODE,bundle);
                return;
            }

            // 跳转到客户详情
            Bundle bundle = new Bundle();
            bundle.putBoolean("isDark",true);
            bundle.putString("title", "客户详情页");
            bundle.putString("path", X3Constent.HOME_CUSETOM_DETAIL_URL + infoBean.carOwnerUserId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
        }
    }

    @Override
    public void iOnClickJumpFirst() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isDark",true);
        bundle.putString("title", "初检报告");
        bundle.putString("path", X3Constent.HOME_FIRST_ALONE_REPORT + receiveBaseId);
        X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
    }

    @Override
    public void iOnClickToSign(String url,String storeName) {

        if (X3StringUtils.isEmpty(url)){
            X3AloneBeforeSignDialog dialog = new X3AloneBeforeSignDialog(this);
            dialog.setiBtnClick(new X3AloneBeforeSignDialog.IBtnClick() {
                @Override
                public void iBtnAffirmClick() {
                    Intent mIntent = new Intent(X3ServiceInfoActivity.this,X3ServiceSignActivity.class);
                    mIntent.putExtra("receiveBaseId",receiveBaseId);
                    mIntent.putExtra("storeName",storeName);
                    startActivityForResult(mIntent,ALONE_SIGN_REQUEST);
                }
            });
            dialog.show();
        } else {
            X3ToastUtils.showMessage(getString(R.string.x3_alone_sign_repeat));
        }

    }
}